package com.dgut.lab.util;

import com.dgut.lab.entity.Answer;
import com.dgut.lab.entity.User;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class CommonUtil {

    public static String escapeExprSpecialWord(String str) {

        if (str != null && str.length() > 0 && str.trim().length() > 0) {

            String[] fbsArr = { "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" };

            for (String key : fbsArr) {

                if (str.contains(key)) {
                    str = str.replace(key, "\\" + key);
                }
            }
        }

        return str;
    }

    public static Date StringToDate(String string) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.parse(string);

    }

    public static String DateToString(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    public static Date DateToDate(Date date) throws ParseException {
        String s = DateToString(date);
        return StringToDate(s);
    }


    public static List<Map<String,Object>> userListToListMap(List<User> users,boolean flag){
        List<Map<String,Object>> result = new ArrayList<>();
        Iterator<User> iterator = users.iterator();
        while(iterator.hasNext()){
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            User user = iterator.next();
            map.put("username",user.getUsername());
            map.put("name",user.getName());
            map.put("idNumber",user.getIdNumber()!=null?user.getIdNumber():"");
            map.put("phone",user.getPhone()!=null?user.getPhone():"");
            map.put("department",user.getDepartment());
            map.put("grade",user.getGrade()!=null?user.getGrade():"");
            map.put("major",user.getMajor()!=null?user.getMajor():"");
            map.put("date",user.getLastCommitTime());
            if(flag){
                map.put("lock",user.getEnabled()==1?false:true);
            }
            result.add(map);
        }
        return result;
    }

    public static List<Map<String,Object>> answersToListMap(List<Answer> answers){
        List<Map<String,Object>> result = new ArrayList<>();
        Iterator<Answer> iterator = answers.iterator();
        while(iterator.hasNext()){
            LinkedHashMap<String,Object> map = new LinkedHashMap<>();
            Answer answer = iterator.next();
            map.put("username",answer.getUsername());
            map.put("address",answer.getAnswers().get("1"));
            map.put("condition", ((String) answer.getAnswers().get("2")).equals("2-1")?"良好":"异常");
            map.put("isArea", ((String) answer.getAnswers().get("3")).equals("3-1")?"否":"是");
            map.put("group",answer.getAnswers().get("4").equals("4-1")?"否":"是");
            map.put("bodyTemperature",answer.getAnswers().get("5"));
            map.put("area",answer.getAnswers().get("3-2-1")!=null?answer.getAnswers().get("3-2-1"):"");
            map.put("date",answer.getDate());
            result.add(map);
        }
        return result;
    }
}
